<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Get</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Get</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>The Get Utility provides a mechanism for attaching script and CSS resources
&mdash; including cross-domain resources &mdash; to the DOM after the page has
loaded.</p>

<p>Common use cases for the Get Utility include:</p>

<ul>
  <li>
  <p><strong>Cross-site data retrieval:</strong> Because XMLHttpRequest and the
  <a href="../io/index.html">YUI IO Utility</a> (which uses
  XMLHttpRequest) adhere to a strict <a
  href="http://en.wikipedia.org/wiki/Same_origin_policy">same-origin
  policy</a>, the retrieval of third-party data via XHR requires a server-side
  proxy.</p>

  <p>When you control or absolutely trust a cross-domain source, you can
  eliminate the server-side proxy by loading a script file directly from the
  external domain. That script file, which would typically contain
  JSON-formatted data, is executed immediately upon load.</p>

  <p>That said, it's crucial to
  understand that if there is malicious code present in the loaded script there
  is no safe way to protect your users from that malicious code; the
  browser will execute the code with full privileges. <strong>Never expose
  your users to JavaScript whose source is not absolutely
  trustworthy.</strong>.</p>
  </li>

  <li>
  <p><strong>Progressive loading of functionality:</strong> In rich web
  applications, it's often useful to load some script and CSS resources only
  when they become necessary (based on user action). The Get Utility provides
  a flexible mechanism for bringing additional resources on-demand.</p>

  <p>If you're loading YUI resources specifically, you may want to use the <a href="../yui/index.html#loader">YUI Loader Utility</a>. Loader employs the Get Utility under the hood to bring in YUI components and has an intrinsic understanding of the YUI dependency tree.</p>
  </li>
</ul>
</div>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for Get and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;get&#x27;, function (Y) {
    &#x2F;&#x2F; Get is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>


<h2 id="using-the-get-utility">Using the Get Utility</h2>

<p>With the Get Utility present, you can make use of it to fetch JavaScript files using <code>Y.Get.script()</code>, and CSS files using <code>Y.Get.css()</code>.  The <code>script()</code> and <code>css()</code> methods each take the following arguments:</p>

<dl>
  <dt>urls (String|String[])</dt>
  <dd>A string or an array of strings designating the URL(s) you wish to load into the page.</dd>

  <dt>options (Object)</dt>
  <dd>An optional object containing configuration information for the transaction; see the <a href="#configuring-a-transaction">Configuring a Transaction</a> section below for the full list of configuraton options you can include here.</dd>
</dl>

<p>A sample request for a file might look like this:</p>

<pre class="code prettyprint">Y.Get.script(&#x27;http:&#x2F;&#x2F;example.com&#x2F;file.js&#x27;, {
  onSuccess: function () {
    Y.log(&#x27;file loaded&#x27;);
  }
});</pre>


<p>If you want to hold onto a transaction object for the request, assign the return value to a variable:</p>

<pre class="code prettyprint">var transaction = Y.Get.script(&#x27;http:&#x2F;&#x2F;example.com&#x2F;file.js&#x27;, {
  onSuccess: function () {
    Y.log(&#x27;file loaded&#x27;);
  }
});</pre>


<p><code>transaction</code> will be an object with a single property, <code>tId</code>, which is a unique identifier of the transaction following the pattern "q0", "q1", "q2", etc.</p>

<p>It's only necessary to store the transaction object if you want to abort the request later.</p>

<h3 id="configuring-a-transaction">Configuring a Transaction</h3>

<p>The Get Utility is configured via the second argument to the <code>script()</code> or <code>css()</code> methods.  This optional argument should be an object containing one or more of the following fields:</p>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td><code>attributes</code></td>
      <td>Object</td>
      <td>A hash of attributes to apply to the dynamically created nodes.  You might use this to add <code>media=&quot;print&quot;</code> to a CSS file, for example.</td>
    </tr>

    <tr>
      <td><code>autopurge</code></td>
      <td>Boolean</td>
      <td>If <code>true</code>, script nodes will automatically be removed every 20 transactions (this number is globally configurable via the <code>Y.Get.PURGE_THRESH</code> property). Script nodes can be safely removed in most cases, as their contents (having executed) remain available. CSS nodes should not have this set to true as it will remove the CSS rules. Default:
      <code>true</code> for script nodes, <code>false</code> for CSS nodes.</td>
    </tr>

    <tr>
      <td><code>context</code></td>
      <td>Object</td>
      <td>The <code>this</code> object to use when executing callbacks. Default: the current YUI instance.</td>
    </tr>

    <tr>
      <td><code>data</code></td>
      <td>Any</td>
      <td>Data to pass as an argument to <code>onSuccess</code> or <code>onFailure</code> callbacks.  Default: <code>null</code>.</td>
    </tr>

    <tr>
      <td><code>onEnd</code></td>
      <td>Function</td>
      <td>Callback function invoked when a transaction completes, no matter how the transaction ended.</td>
    </tr>

    <tr>
      <td><code>onFailure</code></td>
      <td>Function</td>
      <td>Callback function invoked when an error is detected or <code>abort()</code> is called.</td>
    </tr>

    <tr>
      <td><code>onProgress</code></td>
      <td>Function</td>
      <td>Callback function invoked after each node is inserted.</td>
    </tr>

    <tr>
      <td><code>onSuccess</code></td>
      <td>Function</th>
      <td>Callback function invoked when the requested file(s) have loaded successfully.</td>
    </tr>

    <tr>
      <td><code>onTimeout</code></td>
      <td>Function</td>
      <td>Callback function invoked if a timeout is detected.</td>
    </tr>

    <tr>
      <td><code>timeout</code></td>
      <td>Number</td>
      <td>Number of milliseconds to wait for a file to finish loading before timing out.</td>
    </tr>

    <tr>
      <td><code>win</code></td>
      <td>Window</td>
      <td>The window into which the loaded resource(s) should be inserted.  Default: <code>Y.config.win</code>.</td>
    </tr>
  </tbody>
</table>

<h3 id="making-use-of-arguments-supplied-to-your-callback">Making Use of Arguments Supplied to Your Callback</h3>

<p>As noted above, callback methods will have access to the <code>data</code> member supplied in the configuration object, assuming you provided one.  But the <code>data</code> member is just one of several fields included in the object passed to a callback.  Here's a summary of the fields contained in that argument object:</p>

<table>
  <thead>
    <tr>
      <th>Property</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td><code>data</code></td>
      <td>Any</td>
      <td>The <code>data</code> field you passed in the configuration object when the <code>script()</code> or <code>css()</code> method was called. Default: <code>null</code>.</td>
    </tr>

    <tr>
      <td><code>nodes</code></td>
      <td>Array</td>
      <td>An array containing references to DOM node(s) created in processing the transaction.  These will be script nodes for JavaScript and link nodes for CSS.</td>
    </tr>

    <tr>
      <td><code>purge</code></td>
      <td>Function</td>
      <td>Calling the returned <code>purge()</code> method will immediately remove the created nodes. This is safe and prudent for JavaScript nodes, which do not need to persist.  If CSS nodes are purged, the rules they contain are no longer available and the page will repaint accordingly.</td>
    </tr>

    <tr>
      <td><code>tId</code></td>
      <td>String</td>
      <td>The unique identifier for this transaction. This string is available as the <code>tId</code> member of the object returned to you upon calling the <code>script()</code> or <code>css()</code> method.</td>
    </tr>

    <tr>
      <td><code>win</code></td>
      <td>Window</td>
      <td>The window object in which the nodes were created.</td>
    </tr>
  </tbody>
</table>

<p>All of these fields are accessible on the object passed to your <code>onSuccess</code> callback:</p>
<pre class="code prettyprint">Y.Get.script(&#x27;http:&#x2F;&#x2F;json.org&#x2F;json.js&#x27;, {
  onSuccess: function (e) {
    &#x2F;&#x2F; e contains all of the fields described in the table above.

    e.purge(); &#x2F;&#x2F; Removes the script node immediately after executing.
    Y.log(e.data); &#x2F;&#x2F; Logs the data passed in the configuration object.
  },

  data: {
    foo: &#x27;bar&#x27;,
    baz: &#x27;quux&#x27;
  }
});</pre>


<h3 id="using-jsonp-apis">Using JSONP APIs</h3>

<p>A common way to consume a web service that returns JSON data is to use a convention called JSONP. The way it works is that the consumer of the web service supplies the name of a function to execute on the client. The web service response is expected to return a JavaScript response that executes this function and provides a JSON payload as a parameter to the function.</p>

<p>The <a href="../jsonp/index.html">JSONP component</a> provides a simplified API for making JSONP requests using the Get Utility.</p>

<h3 id="how-is-the-get-utility-different-from-io">How is the Get Utility Different From IO?</h3>

<p>The Get Utility inserts new script or CSS content into a window by creating new nodes and supplying a <code>src</code> or <code>href</code> attribute.  Files inserted into the window in this manner are processed (and, in the case of scripts, executed) immediately upon load.</p>

<p>While query parameters can be passed in the URL, no data can be sent to the server via HTTP POST using this method; the Get Utility can only make HTTP GET requests.</p>

<p>As noted above, the Get Utility is ideal for loading your own scripts or CSS progressively (lazy loading) or for retrieving cross-domain JSON data from sources in which you have total trust.</p>

<p>The basic version of the <a href="../io/index.html">IO Utility</a> (<code>io-base</code>) uses the <code>XMLHttpRequest</code> object to interact with the server. <code>XMLHttpRequest</code> is limited by a strict <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>, but it supports a greater range of HTTP methods (including POST).</p> As a result, IO is a more appropriate choice for rich two-way communication between browser and server and gives you more control over data before it's processed within the browser.</p>

<p>The IO Utility also supports cross domain requests through the <code>io-xdr</code> module.  However, there are specific trust requirements as described in the documentation for the <a href="../io/index.html#xdr">IO Utility</a>. The <code>io-xdr</code> submodule may be a better choice than the Get Utility for cross-domain communication if the service you are accessing can be configured to trust the server that is hosting your application.</p>

<h2 id="known-issues">Known Issues</h2>

<ul>
  <li><p>Gecko and WebKit-based browsers (Firefox, Chrome, and Safari) don't fire the DOM <code>load</code> event on a <code>&lt;link&gt;</code> node when a CSS file finishes loading. As a result, the Get Utility fires the <code>onSuccess</code> event immediately in these browsers. A workaround for this issue will be provided in a future release.</p></li>
</ul>
</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using-the-get-utility">Using the Get Utility</a>
<ul class="toc">
<li>
<a href="#configuring-a-transaction">Configuring a Transaction</a>
</li>
<li>
<a href="#making-use-of-arguments-supplied-to-your-callback">Making Use of Arguments Supplied to Your Callback</a>
</li>
<li>
<a href="#using-jsonp-apis">Using JSONP APIs</a>
</li>
<li>
<a href="#how-is-the-get-utility-different-from-io">How is the Get Utility Different From IO?</a>
</li>
</ul>
</li>
<li>
<a href="#known-issues">Known Issues</a>
</li>
</ul>
                    </div>
                </div>
            

            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
